# include <stdio.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <sys/mman.h>
# include <strings.h>
# include <linux/input.h>

int tx,ty,tp;
int flag=0;

struct image{
	char bmppath[20];
	int x;
	int y;
	int w;
	int h;
};

struct image bmp_image[10] = {
	{.bmppath = "1.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "2.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "3.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "4.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "5.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "6.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "7.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "8.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "9.bmp",.x=0,.y=0,.w=800,.h=480},
	{.bmppath = "10.bmp",.x=0,.y=0,.w=800,.h=480}
};

int open_jugg(int i)
{
	//char *j = "oepn_parm";
	//printf("%s=%d\n",j,i);
	if (i< 0)
	{
		//printf("failure\n");
		return 0;
	}
	else
	{
		//printf("success\n");
		return 1;
	}
}


int lcd_display(int x, int y, int w, int h, int b, int g, int r, int tou)
{
	int lcd = open("/dev/fb0",O_RDWR);
	printf("LCD=%d\n",open_jugg(lcd));
	
	int i,j;
	char *buf = mmap(NULL,800*480*4,PROT_WRITE,MAP_SHARED,lcd,0);
	
	for(j=y;j<(y+h);j++)
	{
		for(i=x;i<(x+w);i++)
		{
			buf[0+4*i+800*4*j] = b;
			buf[1+4*i+800*4*j] = g;
			buf[2+4*i+800*4*j] = r;
			buf[3+4*i+800*4*j] = tou;	
			//write(lcd,buf,4);	
		}
		//write(lcd,buf,4*800);
	}
	write(lcd,buf,800*480*4);	
	close(lcd);
	munmap(buf,800*480*4); //释放内存
	return 0;
}

// lcd显示图片 定义指针bmp图片路径 起始坐标 宽 高 透明度
int lcd_dis_bmp(char *bmppath, int x, int y, int w, int h, int tou)
{
	int lcd = open("/dev/fb0",O_RDWR);
	printf("LCD=%d\n",open_jugg(lcd));
	int bmp = open(bmppath,O_RDWR);
	printf("BMP=%d\n",open_jugg(bmp));
	
	lseek(bmp,54,SEEK_SET);
	char bmpbuf[w*h*3];
	read(bmp,bmpbuf,w*h*3);
	
	int i,j;
	char *buf = mmap(NULL,800*480*4,PROT_WRITE,MAP_SHARED,lcd,0);
	
	for(j=y;j<y+h;j++)
	{
		for(i=x;i<x+w;i++)
		{
			buf[0+4*i+800*4*j] = bmpbuf[0+3*(i-x)+w*3*(h-1-j+y)];
			buf[1+4*i+800*4*j] = bmpbuf[1+3*(i-x)+w*3*(h-1-j+y)];
			buf[2+4*i+800*4*j] = bmpbuf[2+3*(i-x)+w*3*(h-1-j+y)];
			buf[3+4*i+800*4*j] = tou;
			//write(lcd,buf,4);	
		}
		//write(lcd,buf,4*800);
	}
	write(lcd,buf,800*480*4);	
	close(lcd);
	close(bmp);
	munmap(buf,800*480*4); //释放内存	
}


 
int touch_scan() //获取一次触摸板坐标
{
	int tsfd = open("/dev/input/event0",O_RDWR);
	printf("TOUCH=%d\n",open_jugg(tsfd));
	struct input_event ts;
	bzero(&ts,sizeof(ts));  //清空数据
	while(1)
	{
		read(tsfd,&ts,sizeof(ts));
		if(ts.type == EV_ABS)  //绝对坐标事件
		{
			if(ts.code == ABS_X)
			{
				tx = ts.value;
				flag++;
				// printf("flag=%d",flag);
			}
			if(ts.code == ABS_Y)
			{
				ty = ts.value;
				flag++;
				// printf("flag=%d",flag);
			}
		}
		
		if (ts.type == EV_KEY)   //压力
		{
			if(ts.code == BTN_TOUCH)
			{
				tp = ts.value;
				flag++;
			}
		}
		
		if (flag == 3)
			{
				flag=0;
				break;
			}			
	}
	return 0;
}

int main(void)
{
	int i = 0;
	while(1)
	{
		touch_scan();
        printf("x=%d,y=%d,p=%d\n",tx,ty,tp);
		if(tx>0&&tx<100&&ty>0&&ty<100)
		{
			printf("LeftUp\n");
			lcd_dis_bmp(bmp_image[i].bmppath,bmp_image[i].x,bmp_image[i].y,bmp_image[i].w,bmp_image[i].h,0);
			i--;
			//sleep(0.5);
			if(i==-1)
			{
				i=9;
			}
		}
		if(tx>700&&tx<800&&ty>0&&ty<100)
		{
			printf("RightUp\n");
			lcd_dis_bmp(bmp_image[i].bmppath,bmp_image[i].x,bmp_image[i].y,bmp_image[i].w,bmp_image[i].h,0);
			i++;
			//sleep(0.5);
			if(i==10)
			{
				i=0;
			}
		}
		
	}
	return 0;
}